/*************************************
 * Created by WebStorm
 * Author : 菲尔敦
 * QQ邮箱：2891044172@qq.com  手机号：15981761402
 * Created Time :2024/2/28 18:45
 Desc :websocket
 **************************************/
import {UseAppStore} from '@/stores/app.ts';
let url = 'wss://xzzh.xzzjyzx.com/webSocket';
// let url = 'ws://0.0.0.0:58080/webSocket';
const appStore = UseAppStore();

interface socket {
    websocket: any
    init: () => any,
    send: (e: any) => void
}

const socket: socket = {
    websocket: null,
    init: () => {
        socket.websocket = new WebSocket(url);
        socket.websocket.onmessage = (e: any) => {
            const data = JSON.parse(e.data);
            if (data.type != undefined) {
                //订单提示
                if (data.type === 1) {
                    appStore.setPaddingOrder(data.count, true)
                } else if (data.type === 2) { //教师提示
                    appStore.setPaddingTeacher(data.count, true)
                } else if (data.type === 3) {//提现申请
                    appStore.setPaddingWOrder(data.count, true)
                }else if (data.type === 0){
                    showOtherNotification(data.message);
                    return false;
                }
                showNotification(data.message,data.count)
            }
        };
        socket.websocket.onclose = (_e: any) => {
            console.log("服务器连接断开！")
        }
    },
    send: (e: any) => {
        if (socket.websocket.readyState == 0) {
            setTimeout(() => {
                socket.websocket.send(JSON.stringify(e))
            }, 1000)
        } else {
            socket.websocket.send(JSON.stringify(e))
        }
    }
}

const showNotification = (message: string, count: number) => {
    const $notification = window['$notification'];
    if (count > 0) {
        $notification.create({
            type: 'warning',
            title: '消息提示',
            content: message,
            duration: 3000,
        })
    }
    console.log("webSocket:",message)
}
const showOtherNotification=(message: string)=>{
    const $notification = window['$notification'];
    $notification.create({
        type: 'warning',
        title: '消息提示',
        content: message,
        duration: 3000,
    })
}

export default socket;